package com.jpro.framework.jackson.core.databind;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;

/**
 * OffsetDateTime反序列化规则
 * <p>
 * 会将毫秒级时间戳反序列化为OffsetDateTime
 */
public class OffsetDateTimeDeserializer extends JsonDeserializer<OffsetDateTime> {

    public static final OffsetDateTimeDeserializer INSTANCE = new OffsetDateTimeDeserializer();

    @Override
    public OffsetDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return OffsetDateTime.ofInstant(Instant.ofEpochMilli(p.getValueAsLong()), ZoneId.systemDefault());
    }
}
